package push2java;

/**
 *
 * @author James Pettit
 */
public class BaseInstructions {

    public static void DUP(State state) {
        if (!state.currentStack().empty()) {
            Object arg1 = state.currentStack().peek();
            state.currentStack().push(arg1);
        }
    }

    public static void POP(State state) {
        if (!state.currentStack().empty()) {
            state.currentStack().pop();
        }
    }

    public static void SWAP(State state) {
        if (state.currentStack().size() >= 2) {
            Object arg1 = state.currentStack().pop();
            Object arg2 = state.currentStack().pop();
            state.currentStack().push(arg1);
            state.currentStack().push(arg2);
        }
    }

    public static void REP(State state) {
        if (state.currentStack().size() >= 2) {
            Object arg1 = state.currentStack().pop();
            state.currentStack().pop();
            state.currentStack().push(arg1);
        }
    }

    public static void EQUALS(State state) {
        if (state.currentStack().size() >= 2) {
            Object arg1 = state.currentStack().pop();
            Object arg2 = state.currentStack().pop();
            state.booleanStack.push(arg1.equals(arg2));
        }
    }

    public static void NOOP(State state) {
    }

    public static void PULL(State state) {
        if (state.integerStack.size() >= 1) {
            Integer index = (Integer) state.integerStack.pop();
            if (index >= 0 && index < state.currentStack().size()) {
                Object arg2 = state.currentStack().remove(index);
                state.currentStack().push(arg2);
            }
        }
    }

    public static void PULLDUP(State state) {
        if (state.integerStack.size() >= 1) {
            Integer index = (Integer) state.integerStack.pop();
            if (index >= 0 && index < state.currentStack().size()) {
                Object arg2 = state.currentStack().get(index);
                state.currentStack().push(arg2);
            }
        }
    }

    /*
    public static void SET(State state) {
        if (!state.nameStack.empty()) {
            String arg1 = (String) state.nameStack.peek();
            Object arg2 = state.currentStack().peek();
            state.currentBindings().put(arg1, arg2);
        }
    }

    public static void GET(State state) {
        if (!state.nameStack.empty()) {
            String arg1 = (String) state.nameStack.peek();
            Object arg2 = state.currentBindings().get(arg1);
            if (arg2 != null) {
                state.currentStack().push(arg2);
            }
        }
    }
     * 
     */

    public static void IF(State state) {
        if (!state.booleanStack.empty() && state.execStack.size() >= 2) {
            Boolean arg1 = (Boolean) state.booleanStack.pop();
            Expression expr1 = (Expression) state.execStack.pop();
            Expression expr2 = (Expression) state.execStack.pop();
            if (arg1) {
                state.execStack.push(expr1);
            } else {
                state.execStack.push(expr2);
            }
        }
    }
}
